﻿using Microsoft.AspNetCore.Mvc;

namespace WebHelper.Controllers
{
    [Route("Book")]
    [ApiController]
    public class BookController : Controller
    {

        [HttpPost]
        [Route("GetBooks")]
        public List<Book>? GetBooks(User user)
        {
            using var db = new DB();
            var list = db.Books.Query().Where(x => x.UserId == user.Id).ToList();
            return list;
        }

        [HttpPost]
        [Route("GetBookRecords")]
        public List<PasswordRecord>? GetBookRecords(Book book)
        {
            using var db = new DB();
            var list = db.PasswordRecords.Query().Where(x => x.BookId == book.Id).ToList();
            return list;
        }

        [HttpPost]
        [Route("SaveBookRecord")]
        public PasswordRecord SaveBookRecord(PasswordRecord book)
        {
            using var db = new DB();
            db.PasswordRecords.Upsert(book);
            return book;
        }

        [HttpPost]
        [Route("SaveBook")]
        public Book SaveBook(Book book)
        {
            using var db = new DB();
            db.Books.Upsert(book);
            return book;
        }        
    }
}
